<template>
  <div class="container">
    <el-form ref="form" :rules="rules" status-icon :model="form">
        <div class="title-container">
          <h3>约车说明设置</h3>
        </div>
        <el-form-item prop="description">
          <el-input type="textarea" v-model="form.description" :rows="15"></el-input>
        </el-form-item>
      <div class="button-container">
        <el-form-item>
          <el-button @click.prevent="update" type="primary" v-bind:loading="loading" class="el-icon-edit">保存</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
  import { getReserve, updateReserve } from '@/api/setting'
    export default {
        name: "reserve",
        inject: ['reload'], //注入reload方法
        data () {
            //自定义校验规则
            var validateDescription = (rule, value, callback) =>  {
                if (value.length < 30) {
                    callback(new Error('约车说明不能少于30个字符'));
                } else {
                    callback();
                }
            };
            return {
                form : {
                    description : ''
                },
                loading: false,
                rules: {
                    description : [
                        {required: true, message: '请输入约车说明', trigger: 'blur'},
                        {min: 30, max: 200, message: '约车长度必须大于30个字符,小于200个字符', trigger: 'change'}
                    ]
                }
            };
        },
        methods : {
            update() {
                this.$refs.form.validate(valid => {
                    this.loading = true;
                    if (valid) {
                        updateReserve({reserve_info : this.form.description}).then(response => {
                            this.$message({
                                message: response.msg,
                                type: 'success'
                            });
                            this.loading = false;
                            this.reload();
                        }).catch(err => {

                        })
                    } else {
                        return false;
                    }
                });
            }
        },
        mounted() {
            getReserve().then(response => {
                this.form.description = response.data.reserveInfo;
            }).catch(err => {

            })
        }
    }
</script>

<style lang="scss" scoped>
    .container {
      min-width: 100%;
      width: 100%;
      overflow: hidden;
      .el-form {
        position: relative;
        width: 600px;
        max-width: 100%;
        margin: 0px auto;
      }
      .title-container {
        width: 600px;
        max-width: 100%;
        h3 {
          font-size: 20px;
          text-align: center;
          height: 20px;
          line-height: 20px;
        }
      }

      .button-container {
        width: 30%;
        max-width: 50%;
        margin: 0px auto;

        .el-button {
          width: 100%;
        }
      }
    }
</style>
